<template>
  <div class="loginwrap">
    <div class="logout" @click="loginOut()" v-if="rk">
      <el-icon><SwitchButton /></el-icon>
      <div class="text2">
        退出登录
      </div>  
    </div>
    <img src="../../assets/img/lbf.jpg" class="bg pcenter" alt="" />
    <img src="../../assets/img/yd.png" class="yd" alt="">
    <div class="subwrap" v-if="rk">
      <div v-for="(item, index) in subList.value" :key="index"
       
      >
        <div  v-if="(rk=='admin-gov')||(rk!='admin-gov'&&item.name=='种植管理')" :class="['rtitle' + index, 'rtitle']">
          <img src="../../assets/img/chang.png" class="chang" alt="" />
          {{ item.name }}
          <img src="../../assets/img/chang.png" class="chang" alt="" />
        </div>
        <div  v-for="(item2, index2) in item.child"
              :key="index2">
          
            <div
              v-if="(rk=='admin-gov')||(rk!='admin-gov'&&item.name=='种植管理')"
              @click="goUrl(item2)"
              :class="['rct' + index + index2, 'rct']"
             
            >
              <div class="rctother">
                <div class="num"
                  v-if="rk=='admin-gov'"
                >0{{ item2.i }}</div>
                <div class="num"
                  v-else
                >0{{ item2.i-4 }}</div>
                <img src="../../assets/img/duan.png" class="duan" alt="" />
                <span class="nameset">
                  {{ item2.name }}
                </span>
              </div>
              <!-- <div class="yw">
                {{ item2.ey }}
              </div> -->
            </div>

          </div>  
       
      </div>
    </div>
    <el-dialog
      v-model="dialogVisible"
      title="请使用微信扫码查看"
      width="300px"
      custom-class="diyclass"
    >
      <div class="ewm">
        <img :src="ewmpic" class="ewmpic" v-if="ewmpic" alt="" />
        <div v-else>暂无信息</div>
      </div>

      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="dialogVisible = false">
            取消
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
  <script lang="ts">
import { defineComponent, onMounted, reactive, ref, computed } from "vue";
import Setting from "@/public/config";
import { post, get } from "@/public/request/api";
import { useRouter } from "vue-router";
import storage from "@/store";
import { ElMessage } from "element-plus";
import { ElMessageBox } from 'element-plus'
import { encrypt, decrypt } from '../../../src/utils/en'
export default defineComponent({
  components: {},
  setup() {
    const router = useRouter();
    console.log('router',router.currentRoute.value.query)
    if(router.currentRoute.value.query.hasOwnProperty('authToken')){
      check(router.currentRoute.value.query.authToken)
    }
    let dialogVisible = ref(false);
    let ewmpic = ref("");
    const token = storage.state.ACCESS_TOKEN
    //const rk=storage.state.userInfo.rk
    let rk = computed(() => {
      return storage.state.userInfo.rk
    })
    console.log('rk---------------',rk)
    let up=storage.state.up||JSON.stringify({
      u:'',
      p:'',
    })
    console.log('up',up);
    let sanUrl=''
    if(up){
      let up2=JSON.parse(up)
      sanUrl='https://iqq.etkqq.gov.cn/farm-base/trace/index?u='+encrypt(up2.u)+'&p='+encrypt(up2.p)
    }else{
      sanUrl='https://iqq.etkqq.gov.cn/farm-base/trace/index'
    }
    console.log('sanUrl',sanUrl)
    let subList = reactive({
      value: [
        {
          name: "宏观决策",
          child: [
            {
              i: 1,
              name: "农业大数据决策分析系统",
              type: "url",
              ey: "Agricultural Big Data Decision Analysis System",
              url: "https://iqq.etkqq.gov.cn/BI",
              // url:'http://139.9.241.190:10007/'
            },
            {
              i: 2,
              name: "农业生产预报系统",
              type: "url",
              ey: "Agricultural Production Forecasting System",
              url: "https://iqq.etkqq.gov.cn/prod",
              //url:'http://139.9.241.190:10004/'
            },
            {
              i: 3,
              name: "特色农产品溯源管理平台",
              type: "url",
              url: sanUrl,
              ey: "Featured agricultural product traceability management platform",
            },
            {
              i: 4,
              name: "农业生产基础数据库",
              type: "url",
              ey: "Agricultural Production Basic Database",
              url: "https://iqq.etkqq.gov.cn/base-data",
              //url:'http://139.9.241.190:10011/'
            },
          ],
        },
        {
          name: "种植管理",
          child: [
            {
              i: 5,
              name: "农场基础管理系统",
              type: "url",
              ey: "Farm Basic Management System",
            },
            {
              i: 6,
              name: "决策分析大屏",
              type: "url",
              ey: "Farm Basic Management System",
            },
            // {
            //   i: 6,
            //   name: "玉米智慧管理小程序",
            //   ey: "Corn Wisdom Management Mini Program",
            //   type: "ewm",
            //   pic: require("../../assets/img/xcxzz.jpg"),
            // },
            // {
            //   i: 7,
            //   name: "一粒米乡村振兴溯源小程序",
            //   ey: "Corn Wisdom Management Mini Program",
            //   type: "ewm",
            //   pic: require("../../assets/img/xcxsy.jpg"),
            // },
          ],
        },
        {
          name: "养殖防护",
          child: [
            {
              i: 8,
              name: "动物疫病防控平台",
              type: "url",
              url: "http://222.74.141.5:7071",
              ey: "Animal Disease Prevention and Control Platform",
            },
          ],
        },
      ],
    });
    onMounted(() => {
      let userId = storage.state.userInfo.userId;
      console.log("userId", userId);
      // if (userId == 5) {
      //   subList.value = [
      //     {
      //       name: "宏观决策",
      //       child: [
      //         {
      //           i: 1,
      //           name: "农业大数据决策分析系统",
      //           type: "url",
      //           ey: "Agricultural Big Data Decision Analysis System",
      //           url: "https://iqq.etkqq.gov.cn/BI?token="+token,
      //         },
      //         {
      //           i: 2,
      //           name: "农业生产预报系统",
      //           type: "url",
      //           ey: "Agricultural Production Forecasting System",
      //           url: "https://iqq.etkqq.gov.cn/prod?token="+token,
      //         },
      //       ],
      //     },
      //   ];
      // }
    });
    function loginOut(){
      ElMessageBox.confirm('确认退出当前账号?')
        .then(() => {
          storage.commit('changeACCESS_TOKEN','')
          sessionStorage.setItem('userInfo','')
          sessionStorage.setItem('farmList', '')
          sessionStorage.setItem('nowBaseId','')
          router.push({
            path:'/user/login'
          })
        })
        .catch(() => {
          // catch error
        })
    }
    function goUrl(item) {
      if (item.type == "url") { 
        if (item.name == "农场基础管理系统") {
          window.open(
            // 'http://139.9.241.190:10003/#/farmlandData/statistics'
            process.env.NODE_ENV === 'development'?"http://localhost:8080/#/farmlandData/statistics":'http://139.9.241.190:10003/#/farmlandData/statistics'
           //'https://iqq.etkqq.gov.cn/farm-base/#/farmlandData/statistics'
          );
        }else if (item.name == "决策分析大屏") {
          window.open(
            // 'http://139.9.241.190:10003/#/farmlandData/statistics'
            process.env.NODE_ENV === 'development'?"http://139.9.241.190:10007":'http://139.9.241.190:10007'
           //'https://iqq.etkqq.gov.cn/farm-base/#/farmlandData/statistics'
          );
        }else {
          window.open(item.url);
        }
      }
      if (item.type == "ewm") {
        dialogVisible.value = true;
        ewmpic.value = item.pic;
      }
    }
    function check(token){
      console.log(123321);
      
      
      post("/checkThirdPartyToken", {
        token,
      }).then((res) => {
        console.log('res',res);
        
        storage.commit('changeACCESS_TOKEN',res.data.token)
        getInfo()
      }).catch(err=>{
        console.log('errr',err);
        ElMessage.error(err.message)
        setTimeout(() => {
          router.push({
            path:'/user/login'
          })
        }, 1000);
      })
    }
    function getInfo(){
      get('/loginUserInfo',{

      }).then(res=>{
        let baseId=null
        let baseName=null
        let id=sessionStorage.getItem('nowBaseId')
        if(id){
          if(res.data.baseAuths.length>0){
            let findone=res.data.baseAuths.find(item=>{
              return item.id==id
            })
            if(findone){
              baseId=id
              baseName=findone.name
            }else{
              baseId=res.data.baseAuths[0]['id']
              baseName=res.data.baseAuths[0]['name']
            }
          }
        }else{
          if(res.data.baseAuths.length>0){
              baseId=res.data.baseAuths[0]['id']
              baseName=res.data.baseAuths[0]['name']
          }
      
        }
        storage.commit('changeUserInfo',{
          userId:res.data.userId,
          name:res.data.fullName,
          rk:res.data.roles[0]['roleKey'],
          baseId:baseId,
          baseName:baseName,
          allBase:res.data.baseAuths
        })
        storage.commit('changeGlobalLatifundiumId',res.data.userId)
        storage.commit('changeUserRole',res.data.roles[0]['roleKey'])


        // if(res.data.roles[0]['roleKey']!='admin'){
          // router.push({
          //  path:'/user/subPage'
          // })
        // }else{
        //   getFarmList()
        // }
      })
    }

    return {
      check,
      rk,
      loginOut,
      subList,
      goUrl,
      ewmpic,
      dialogVisible,
    };
  },
});
</script>
  <style lang="scss" scoped>
@import "@/assets/scss/index.scss";
.logout{
  position: fixed;
  top: 20px;
  right: 20px;
  color: #fff;
  display: flex;
  align-items: center;
  cursor: pointer;
  z-index: 9999999;
  .text2{
    margin-left: 5px;
    position: relative;
    z-index: 9999999;
  }
}
.ewmpic {
  width: 200px;
}
.ewm {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 20px;
}
.subwrap {
  width: 40%;
  height: 83%;

  z-index: 99;
  right: -26%;
  top: 51%;
  bottom: 50%;
  transform: translate(-50%, -50%);
  position: absolute;
  display: flex;
  flex-direction: column;
  justify-content: center;
  
  .rctother {
    display: flex;
    align-items: center;
  }
  .rct {
    cursor: pointer;
  }
  .rct01 {
    margin-left: 30px;
  }
  .rct02 {
    margin-left: 60px;
  }
  .rct03 {
    margin-left: 90px;
  }
  .rtitle1 {
    margin-left: 120px;
  }

  .rct10 {
    margin-left: 90px;
  }
  .rct11 {
    margin-left: 60px;
  }
  .rct12 {
    margin-left: 30px;
  }
  .rtitle2 {
    margin-left: 30px;
  }
  .duan {
    width: 35px;
    margin: 0 5px;
  }
  @media (max-width: 1600px) {
    .nameset {
     font-size: 26px!important;
    }
    .rtitle{
      font-size: 26px!important;
    }
    .yw{
      display: none!important;
    }
    .subwrap{
      right: -11%!important;
    }
  }
  .nameset {
    font-size: 36px;
    font-family: YouSheBiaoTiHei-Regular, YouSheBiaoTiHei;
    font-weight: 400;
    color: #0e9ae9;
  }
  .yw {
    font-size: 18px;
    font-family: D-DIN-DIN-Bold, D-DIN-DIN;
    font-weight: bold;
    color: #0e9ae9;
    margin-bottom: 10px;
  }
  .num {
    font-size: 48px;
    font-family: D-DIN-DIN-Italic, D-DIN-DIN;
    font-weight: normal;
    color: #e1f2fc;
    font-style: italic;
  }
  .rtitle {
    display: flex;
    align-items: center;
    font-size: 32px;
    font-family: YouSheBiaoTiHei-Regular, YouSheBiaoTiHei;
    font-weight: 400;
    color: #064199;

    .chang {
      width: 120px;
    }
  }
}
.loginwrap {
  width: 100vw;
  height: 100vh;
  position: relative;
  .overtrue {
    background: #ccc !important;
  }
  .yzmpic {
    width: 120px;
    height: 40px;
    cursor: pointer;
  }
  .bnotice {
    color: rgba(250, 252, 255, 1);
    margin-top: 50px;
    text-align: center;
  }
  .pcenter {
    top: 50%;
    right: 50%;
    left: 50%;

    bottom: 50%;
    transform: translate(-50%, -50%);
    position: absolute;
  }
  .w1 {
    padding: 5px 15px;
    flex: 1;
    margin-right: 12px;
  }
  .inputd3 {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .submit {
    background: rgba(24, 142, 245, 1);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 45px;
    font-size: 16px;
    color: #fff;
    margin-top: 30px;
    cursor: pointer;
  }
  .infop {
    background: rgba(255, 255, 255, 0.16);
    border-radius: 15px;
    padding: 40px 15px;
    width: 535px;
    height: 500px;
    z-index: 99;
    right: 10% !important;
    left: 82%;
    .flexdiv {
      display: flex;
      align-items: center;
    }
    .inputd {
      display: flex;
      align-items: center;
      padding: 5px 15px;
      margin-bottom: 15px;
    }
    .st {
      color: #fff;
      font-size: 16px;
      margin-bottom: 10px;
    }
    .inputd2 {
      justify-content: space-between;
    }
    .white {
      background: #fff;
      border-radius: 8px 8px 8px 8px;
      opacity: 1;
      border: 1px solid #188ef5;
    }
  }
  .swz {
    background: linear-gradient(180deg, #ffffff 0%, #0e9ae9 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    font-size: 21px;
    font-weight: bold;
    margin-bottom: 40px;
  }
  .bg {
    position: absolute;

    width: 100%;
  }
  .yd{
    width: 200px;
    position: fixed;
    right: 1.5%;
    bottom: 3%;
  }
}
</style>